/*
package com.ruiyi.subway.utils;

import com.ruiyi.subway.exception.EncryptException;
import org.springframework.util.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;

*/
/**
 * 所有加解密、字符串转换相关方法
 * Created by zhangruiqian on 16/10/24.
 *//*

public class EncryptUtil {
    //TODO 添加加密key配置项
    private static final String AESKey = "Happy20161234567";
    private static final String ENCRYPT_ALG = "DES";
    private static final Base64 base64 = new Base64(true);

    */
/**
     * 加密
     *
     * @param content 需要加密的内容
     * @return
     *//*

    public static String encryptAES(String content) throws Exception {
        return encryptAES(content, AESKey); // 加密
    }

    */
/**
     * 带密钥加密
     *
     * @param sSrc
     * @param sKey
     * @return
     * @throws Exception
     *//*

    public static String decryptAES(String sSrc, String sKey) throws EncryptException {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] sSrcTmp = parseHexStr2Byte(sSrc);
            sSrc = new Base64().encodeToString(sSrcTmp);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }

    }

    */
/**
     * 解密
     *
     * @param content 待解密内容
     * @return
     *//*

    public static String decryptAES(String content) throws EncryptException {
        return decryptAES(content, AESKey); // 加密
    }

    */
/**
     * 带密钥加密
     *
     * @return
     * @throws Exception
     *//*

    public static String encryptAES(String sSrc, String sKey) throws Exception{
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return parseByte2HexStr(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。

    }


    */
/**
     * 根据密钥获得 SecretKeySpec
     *
     * @return
     *//*

    private static SecretKeySpec genKey(String key) throws EncryptException {
        byte[] enCodeFormat = {0};
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(key.getBytes());
            kgen.init(128, secureRandom);
            SecretKey secretKey = kgen.generateKey();
            enCodeFormat = secretKey.getEncoded();

        } catch (Exception e) {
            e.printStackTrace();
            throw new EncryptException("加密过程失败", e);
        }

        return new SecretKeySpec(enCodeFormat, "AES");
    }


    */
/**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     *//*

    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    */
/**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     *//*

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public String generateRandomPass() {
        int i = UUID.randomUUID().toString().hashCode();
        return String.format("%06d", Math.abs(i) % 1000000);
    }


    public static String encodeBase64(byte[] content) {
        return base64.encodeAsString(content);
    }

    public static byte[] decodeBase64(String content) {
        return base64.decode(content);
    }

    */
/**
     * MD5加密
     *
     * @param content
     * @return
     * @throws Exception
     *//*

    public static String encryptMD5(String content) throws EncryptException {
        try {
            return DigestUtils.md5Hex(content);
        } catch (Exception e) {
            throw new EncryptException("MD5加密出现错误");
        }
    }

    public static byte[] encryptMD5(byte[] inputByteArray) throws EncryptException {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(inputByteArray);
            return messageDigest.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new EncryptException("加密过程失败", e);
        }
    }

    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPT_ALG);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ENCRYPT_ALG);
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPT_ALG);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ENCRYPT_ALG);
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

}
*/
